

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user
 
set graphics on  

global path_data_processed "\data\processed"
global path_output "\data\output"

 
clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 

gen ind_cyclical=1-health_educ-ind_other
tab ind_cyclical, m
gen ind_noncyclical=1-ind_cyclical

loc experiments "unemp_prob unemp_prob unemp_prob unemp_prob unemp_prob unemp_prob unemp_prob"
	   
gen posterior_educ = posterior*atleast_bachelor
gen tr_educ = treatment*atleast_bachelor

gen posterior_old = posterior*old
gen tr_old = treatment*old

gen posterior_female = 	posterior*female
gen tr_female = treatment*female

gen posterior_highearn = posterior*highearn
gen tr_highearn = treatment*highearn

gen posterior_ever_unemp = posterior*ever_unemployed_imp
gen tr_ever_unemp = treatment*ever_unemployed_imp
gen posterior_never_unemp = posterior*never_unemployed_imp
gen tr_never_unemp = treatment*never_unemployed_imp

gen posterior_highcountyunemp = posterior*highunemp_rate3y
gen tr_highcountyunemp = treatment*highunemp_rate3y
gen posterior_lowcountyunemp = posterior*(1-highunemp_rate3y)
gen tr_lowcountyunemp = treatment*(1-highunemp_rate3y)

gen posterior_ind_cyclical = posterior*ind_cyclical
gen tr_ind_cyclical = treatment*ind_cyclical
gen posterior_ind_noncyclical = posterior*(1-ind_cyclical)
gen tr_ind_noncyclical = treatment*(1-ind_cyclical)


foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index
			   
preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

loc ++columns
qui eststo col`columns': reg x y

}

restore

loc colnum = 1
loc colnames ""

*** Column 1: Education

ivreg2 unemp_prob (posterior posterior_educ = treatment tr_educ) $controls, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_educ, prec(3)
estadd loc thisstat5 = "`r(bstar)'": col`colnum'
estadd loc thisstat6 = "`r(sestar)'": col`colnum'

test posterior + posterior_educ = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 2: Earnings

ivreg2 unemp_prob (posterior posterior_highearn = treatment tr_highearn) $controls highearn, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_highearn, prec(3)
estadd loc thisstat8 = "`r(bstar)'": col`colnum'
estadd loc thisstat9 = "`r(sestar)'": col`colnum'

test posterior + posterior_highearn = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 3: Age

ivreg2 unemp_prob (posterior posterior_old = treatment tr_old) $controls old, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_old, prec(3)
estadd loc thisstat11 = "`r(bstar)'": col`colnum'
estadd loc thisstat12 = "`r(sestar)'": col`colnum'

test posterior + posterior_old = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 4: Gender

ivreg2 unemp_prob (posterior posterior_female = treatment tr_female) $controls, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_female, prec(3)
estadd loc thisstat14 = "`r(bstar)'": col`colnum'
estadd loc thisstat15 = "`r(sestar)'": col`colnum'

test posterior + posterior_female = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 5: Industry

ivreg2 unemp_prob (posterior posterior_ind_noncyclical = treatment tr_ind_noncyclical) ind_cyclical $controls, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_ind_noncyclical, prec(3)
estadd loc thisstat17 = "`r(bstar)'": col`colnum'
estadd loc thisstat18 = "`r(sestar)'": col`colnum'

test posterior + posterior_ind_noncyclical = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob if ind_cyclical!=.
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 6: Personal unemployment history

ivreg2 unemp_prob (posterior posterior_never_unemp = treatment tr_never_unemp) never_unemployed_imp $controls, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_never_unemp, prec(3)
estadd loc thisstat20 = "`r(bstar)'": col`colnum'
estadd loc thisstat21 = "`r(sestar)'": col`colnum'
	
test 	posterior + posterior_never_unemp = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob if never_unemployed_imp!=.
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 7: County-level unemployment

ivreg2 unemp_prob (posterior posterior_lowcountyunemp = treatment tr_lowcountyunemp) highunemp_rate3y $controls, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_lowcountyunemp, prec(3)
estadd loc thisstat23 = "`r(bstar)'": col`colnum'
estadd loc thisstat24 = "`r(sestar)'": col`colnum'
	
test 	posterior + posterior_lowcountyunemp = 0
estadd loc thisstat26 = string(r(p), "%9.3f"): col`colnum'

qui sum unemp_prob if highunemp_rate3y!=.
estadd loc thisstat28 = r(N): col`colnum'
*estadd loc thisstat29 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat30 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat31 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc rowlabels " " " "Posterior: Recession (a)" " " " " "Posterior: Recession $\times$ (b) " "High education" " " "Posterior: Recession $\times$ (b) " "High earnings" " " "Posterior: Recession $\times$ (b) " "Age$>$44" " " "Posterior: Recession $\times$ (b) " "Female" " " "Posterior: Recession $\times$ (b) " "Non-cyclical industry" " " "Posterior: Recession $\times$ (b) " "Never unemployed"   " " "Posterior: Recession $\times$ (b) " "Low county unemployment" " " "Pr(a+b)=0" " " "Observations" " " "First stage F-stat (a)" "First stage F-stat (b)" " "   "\hline" " "   "


loc rowstats ""
loc rowstats ""

forval i = 1/31 {
loc rowstats "`rowstats' thisstat`i'"
}

esttab * using "nationalunemp_het_interact.tex", replace cells(none) booktabs nonotes nomtitles compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
mgroups("National unemployment (percent)"  , pattern(1 0 0 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

eststo clear	























